home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / vdigit.zip / VPTEST.C < prev    next >
C/C++ Source or Header  |  1989-06-24  |  4KB  |  154 lines

  1. /****************************************************************/
  2. /*                                                              */
  3. /*           Digitized Voice Programmer's Toolkit               */
  4. /*           ------------------------------------               */
  5. /*                                                              */
  6. /*        Example of a program which plays voice data           */
  7. /*             from a file of unlimited length                  */
  8. /*                                                              */
  9. /*          Copyright (c) 1989, Farpoint Software               */
  10. /*                                                              */
  11. /*                                                              */
  12. /*  This program simply plays a voice message while displaying  */
  13. /*  a message repeatedly indicating the count of bytes played.  */
  14. /*  The digitized voice data is read from a disk file piece by  */
  15. /*  piece while the playback is in progress. The memory buffer  */
  16. /*  used is much smaller than the potential file size. Data is  */
  17. /*  read from the buffer with the pointer wrapping back to the  */
  18. /*  beginning of the buffer upon reaching the end. The routine  */
  19. /*  PVOICE_CATCHUP (source in VPMOD.ASM) transfers data from    */
  20. /*  the file to the buffer in the same way.                     */
  21. /*                                                              */
  22. /****************************************************************/
  23.  
  24. #include "vpmod.h"
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <malloc.h>
  29. #include <fcntl.h>
  30. #include <io.h>
  31. #include <sys\types.h>
  32. #include <sys\stat.h>
  33. #include <bios.h>
  34. #include <string.h>
  35.  
  36. #define buf_size  16384L
  37.  
  38. char filename[128];
  39. char huge *buffer;
  40. int handle;
  41.  
  42. main(argc,argv)
  43. int argc;
  44. char **argv;
  45.  
  46. {
  47. short retval;
  48. long count,index;
  49.  
  50. /* check for one argument on command line */
  51.  
  52. if (argc != 2)
  53.     {
  54.     printf("Include file name on command line.\n");
  55.     exit(1);
  56.     }
  57.  
  58. argv++;
  59. strcpy(filename,*argv);
  60.  
  61. /* open the file */
  62.  
  63. handle = open(filename,O_BINARY|O_RDONLY);
  64. if (handle == -1)
  65.     {
  66.     printf("Error opening file.\n");
  67.     exit(1);
  68.     }
  69.  
  70. /* allocate memory for the buffer */
  71.  
  72. buffer = (char huge *)halloc(buf_size,1);
  73. if (buffer == NULL)
  74.     {
  75.     close(handle);
  76.     printf("Unable to allocate buffer memory.\n");
  77.     exit(1);
  78.     }
  79.  
  80. /* get ready */
  81.  
  82. PVOICE_INIT();
  83.  
  84. /* start the playback */
  85.  
  86. retval = PVOICE_START(buffer,(long)buf_size,1,handle,filelength(handle),0L);
  87.  
  88. /* test return code for success or failure */
  89.  
  90. switch (retval)
  91.     {
  92.     case 0:
  93.         printf("Voice playback has begun.\n");
  94.         break;
  95.     case 1:
  96.     case 2:
  97.         printf("Buffer sizing error.\n");
  98.         break;
  99.     case 3:
  100.         printf("File read error.\n");
  101.     }
  102.  
  103. if (!retval)
  104.     {
  105.  
  106.     /* loop while waiting for the process to complete */
  107.  
  108.     while (PVOICE_STATUS(&count,&index))
  109.         {
  110.  
  111.         /* If PVOICE_CATCHUP returns non-zero then file read error. */
  112.         /* PVOICE_CATCHUP has to be called frequently during playback */
  113.         /*  in order to keep reading the file and keep the data in    */
  114.         /*  the buffer current.                                       */
  115.  
  116.         if (PVOICE_CATCHUP())
  117.             {
  118.             printf("File read error.\n");
  119.             break;
  120.             }
  121.         else
  122.  
  123.             /* just something to do in the foreground */
  124.  
  125.             printf("Number of bytes played = %ld\n",count);
  126.  
  127.         /* check the keystroke buffer */
  128.  
  129.         if (_bios_keybrd(_KEYBRD_READY))
  130.             {
  131.  
  132.             /* keystroke found, test for <Esc> key */
  133.  
  134.             if ( (_bios_keybrd(_KEYBRD_READ) >> 8) == 0x01 )
  135.                 {
  136.                 PVOICE_STOP();
  137.                 printf("Voice playback stopped with <Esc>\n");
  138.                 break;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144. /* do this before exiting or suffer the consequences */
  145.  
  146. PVOICE_CLEANUP();
  147.  
  148. /* close the file and free the allocated memory */
  149.  
  150. close(handle);
  151. free(buffer);
  152.  
  153. }
  154.